package cn.ty.znsz.role;

import com.jfinal.aop.Before;
import com.jfinal.aop.Inject;
import com.jfinal.core.Controller;
import com.jfinal.core.Path;
import com.jfinal.kit.JsonKit;
import com.jfinal.kit.Ret;
import com.jfinal.plugin.activerecord.Page;

import cn.ty.znsz.common.interceptor.PostInterceptor;
import cn.ty.znsz.common.model.SysRole;
import cn.ty.znsz.user.UserService;

@Path(value="/role")
public class RoleController extends Controller{
	
	@Inject 
	UserService us;
	@Inject 
	RoleService rs;
	
	/**
	 * 首页
	 */
	public void index() {
		String cookie=getCookie("jznsz");
		//查询菜单权限
		Ret ret=us.getMenuPoint(getParaToInt("id"),cookie);
		setAttr("powers", ret.get("data"));
		render("index.html");
	}
	/**
	 * 首页分页
	 */
	public void paginate() {
		Page<SysRole> page=rs.paginate(getParaToInt("pageNumber"), getParaToInt("pageSize"),getPara("roleName"));
		renderJson(Ret.create().set("msg", "操作成功").set("code", 0).set("count", page.getTotalRow()).set("data", page.getList()));
	}	
	/**
	 * 跳转新增页面
	 */
	public void roleAdd() {
		render("roleAdd.html");
	}
	/**
	 * 添加角色保存
	 */
	@Before(PostInterceptor.class)
	public void roleSave() {
		SysRole data=getModel(SysRole.class, "data");
		Ret ret=rs.roleSave(data);
		renderJson(ret);
	}
	/**
	 * 跳转编辑页面
	 */
	public void roleEdit() {
		render("roleEdit.html");
	}
	/**
	 * 修改角色保存
	 */
	@Before(PostInterceptor.class)
	public void roleUpdate() {
		SysRole data=getModel(SysRole.class, "data");
		Ret ret=rs.roleUpdate(data,getParaToInt("id"));
		renderJson(ret);
	}
	/**
	 * 删除角色
	 */
	@Before(PostInterceptor.class)
	public void roleDel(){
		Ret ret=rs.roleDel(getParaToInt("id"));
		renderJson(ret);
	}
	/**
	 * 后台角色授权
	 */
	public void powerWeb() {
		keepPara("id");
		Ret ret=rs.powerWeb(getParaToInt("id"));
		setAttr("menus", JsonKit.toJson(ret.get("data")));
		render("powerWeb.html");
	}
	/**
	 * 后台角色授权保存
	 */
	@Before(PostInterceptor.class)
	public void savePowerWeb() {
		Ret ret=rs.savePowerWeb(getPara("checkData"),getParaToInt("roleId"));
		renderJson(ret);
	}
}
